layui.use(['table', 'laypage'], function(){
    let table = layui.table

    table.render({
        elem: '#userlist',
        url: apiurl + "/sys/user",
        toolbar: '#titlebar',
        cols: [[
            {field: 'id', title: '', type: 'checkbox', align: 'center'},
            {field: 'id', title: 'ID', width: 80, sort: true, align: 'center'},
            {field: 'username', title: '用户名'},
            {field: 'typename', title: '类型', width: 100},
            {field: 'flagname', title: '开关', width: 100},
            {field: 'empname', title: '姓名', width: 100},
            {field: 'mobilephone', title: '手机', width: 120},
            {fixed: 'right', title:'操作', toolbar: '#colbar', width:220, align: 'center'},
        ]],
        page: true,
        even: true,
        limits: [10, 15, 20],
        limit:10,
        parseData: function(res) {
            let fv = $("#qryContent").val()
            let resx = res.filter((item) => {
                return (
                    item.id.toString().indexOf(fv) >= 0 ||
                    item.username != null && item.username.indexOf(fv) >= 0 ||
                    item.empname != null && item.empname.indexOf(fv) >= 0 ||
                    item.flagname.indexOf(fv) >= 0 ||
                    item.mobilephone != null && item.mobilephone.indexOf(fv) >= 0 ||
                    item.typename.indexOf(fv) >= 0
                )
            })
            res = resx

            let rst

            if (this.page.curr) {
                rst = res.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr)
            }
            else {
                rst = res.slice(0, this.limit)
            }

            return {
                "code": 0,
                "data": rst,
                "count": res.length,
                "msg": res.message
            }
        },
    })

    table.on('toolbar(userlist)', function(obj) {
        switch (obj.event) {
            case 'qryCmd':
                let fv = $("#qryContent").val()
                $.getJSON(apiurl + "/sys/user", null, function(data) {
                    let fdata = data.filter((item) => {
                        return (
                            item.id.toString().indexOf(fv) >= 0 ||
                            item.username != null && item.username.indexOf(fv) >= 0 ||
                            item.empname != null && item.empname.indexOf(fv) >= 0 ||
                            item.flagname.indexOf(fv) >= 0 ||
                            item.mobilephone != null && item.mobilephone.indexOf(fv) >= 0 ||
                            item.typename.indexOf(fv) >= 0
                        )
                    })

                    table.render({
                        elem: '#userlist',
                        toolbar: '#titlebar',
                        cols: [[
                            {field: 'id', title: '', type: 'checkbox', align: 'center'},
                            {field: 'id', title: 'ID', width: 80, sort: true, align: 'center'},
                            {field: 'username', title: '用户名'},
                            {field: 'typename', title: '类型', width: 100},
                            {field: 'flagname', title: '开关', width: 100},
                            {field: 'empname', title: '姓名', width: 100},
                            {field: 'mobilephone', title: '手机', width: 120},
                            {fixed: 'right', title:'操作', toolbar: '#colbar', width:220, align: 'center'},
                        ]],
                        page: true,
                        even: true,
                        curr: 1,
                        limit: 10,
                        limits: [10, 15, 20],
                        data: fdata
                    })

                    $("#qryContent").val(fv)
                })
                break
        }
    })



})
